<template>
  <el-container class="home-container">
    <!-- 头部区 -->
    <el-header
      ><div>
        <img src="../assets/image.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <!-- this.userData.username -->
      <div class="el-dropdown-a">
        <el-avatar
          ><template>
            <span>{{ userInfo.firstName }}</span>
            <!-- 动态显示字符 -->
          </template></el-avatar
        >
        <el-dropdown @command="logout">
          <span class="el-dropdown-link">
            {{ userInfo.username
            }}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item disabled>
              {{ userInfo.roleName }}
            </el-dropdown-item>
            <el-dropdown-item>退出</el-dropdown-item>
            <!-- <el-button type="info" ></el-button> -->
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <!-- 主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单栏 -->
        <el-menu
          :unique-opened="true"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409fff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本 -->
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由站位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      username: '',
      rolesList: [],
    }
  },
  computed: {
    // 使用对象展开运算符将 getter 混入 computed 对象中
    ...mapGetters([
      // 'firstName',
      // 'roleName',
      'userInfo',
      // ...
    ]),
  },
  mounted() {
    this.getMenulist()
    this.activePath = window.sessionStorage.getItem('activePath')
    this.$store.dispatch('getRolesList')
    // console.log(this.$store.state)
  },

  methods: {
    logout() {
      // 清空sessionStorage中的token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenulist() {
      const ress = await this.$http.get('menus')
      const { data } = ress
      const res = data
      console.log(data)

      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.menulist = res.data
      this.menulist.forEach((i) => {
        if (i.id === 145) {
          console.log(i)
          i.children.push({ authName: '布局练习1', id: 147, path: 'reportss' })
        }
      })
    },
    // 点击按钮菜单折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
  },
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #e7eaec;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin: 5px;
    }
  }
}
.el-aside {
  display: flex;
  flex-direction: column;
  transition-property: all;
  transition-duration: 0.5s;
  .el-menu {
    border-right: none;
    flex: 1;

    // height: calc(100% - 24px);
  }
  background-color: #373d41;
}
.el-main {
  background-color: #e7eaec;
  padding: 12px;
  min-width: 1000px;
}
.home-container {
  display: flex;
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-avatar {
  font-size: 20px;
  background-color: rgb(113, 233, 189);
}
.el-dropdown-link {
  cursor: pointer;
  color: rgb(243, 243, 243);
}
.el-dropdown-a {
  margin: 5px;
  text-align: center;
}
</style>
